package com.beiing.tianshuai.tianshuai.message.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.MyFriendListBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/9/22 0022.
 */

public class GroupAddMembersModel implements GroupAddMembersModelImpl{

    private static final String TAG = "GroupAddMembersModel";
    private OnRequestListener mListener;

    public GroupAddMembersModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getAddMembersRequestResponse(String gid, String easeGid, String fId, String tId, String isOwner) {
        Observable<CodeBean> observable = DataManager.getGroupAddMembersRequestResponse(
                gid,
                easeGid,
                fId,
                tId,
                isOwner
        );
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getAllConstantsRequestResponse(String uid) {
        Observable<MyFriendListBean> loginResult = DataManager.getMyFriendList(
                uid
        );
        Observable<MyFriendListBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<MyFriendListBean>() {

            @Override
            public void onNext(@NonNull MyFriendListBean bean) {
                if (mListener != null) {
                    mListener.onGetConstantsSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener{
        void onSuccess(CodeBean bean);
        void onGetConstantsSuccess(MyFriendListBean bean);
        void onError(Throwable throwable);
    }
}
